//-----------------------------------------------------------------------
// <copyright file="tblFileUploadDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblFileUploadDAOBase
    {
        #region Common methods
        public virtual tblFileUpload CreatetblFileUploadFromReader(IDataReader reader)
        {
            tblFileUpload item = new tblFileUpload();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (Guid)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"FileName")) item.FileName = (string)reader["FileName"];
                if (UtilityDAO.CheckColumnData(reader,"UploadDate")) item.UploadDate = (DateTime)reader["UploadDate"];
                if (UtilityDAO.CheckColumnData(reader,"UploadBy")) item.UploadBy = (string)reader["UploadBy"];
                if (UtilityDAO.CheckColumnData(reader,"Path")) item.Path = (string)reader["Path"];
                if (UtilityDAO.CheckColumnData(reader,"FileType")) item.FileType = (int)reader["FileType"];
                if (UtilityDAO.CheckColumnData(reader,"Status")) item.Status = (int)reader["Status"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblFileUploadFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblFileUpload methods
            
        public virtual void CreatetblFileUpload(tblFileUpload tblFileUpload)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblFileUploadInsert");
                
                database.AddInParameter(dbCommand, "@FileName", DbType.AnsiString, tblFileUpload.FileName);
                database.AddInParameter(dbCommand, "@UploadDate", DbType.DateTime, tblFileUpload.UploadDate);
                database.AddInParameter(dbCommand, "@UploadBy", DbType.AnsiString, tblFileUpload.UploadBy);
                database.AddInParameter(dbCommand, "@Path", DbType.AnsiString, tblFileUpload.Path);
                database.AddInParameter(dbCommand, "@FileType", DbType.Int32, tblFileUpload.FileType);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblFileUpload.Status);
                database.AddOutParameter(dbCommand, "@ID", DbType.Guid, 16);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblFileUpload.ID = (Guid)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblFileUploadException, ex);
            }
        }

        #endregion

        #region UpdatetblFileUpload methods
        
        public virtual void UpdatetblFileUpload(tblFileUpload tblFileUpload)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblFileUploadUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, tblFileUpload.ID);
                database.AddInParameter(dbCommand, "@FileName", DbType.AnsiString, tblFileUpload.FileName);
                database.AddInParameter(dbCommand, "@UploadDate", DbType.DateTime, tblFileUpload.UploadDate);
                database.AddInParameter(dbCommand, "@UploadBy", DbType.AnsiString, tblFileUpload.UploadBy);
                database.AddInParameter(dbCommand, "@Path", DbType.AnsiString, tblFileUpload.Path);
                database.AddInParameter(dbCommand, "@FileType", DbType.Int32, tblFileUpload.FileType);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblFileUpload.Status);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblFileUploadException, ex);
            }
        }
        
        #endregion

        #region DeletetblFileUpload methods
        public virtual void DeletetblFileUpload(Guid id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblFileUploadDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblFileUploadException, ex);
            }
        }

        #endregion

        #region GettblFileUpload methods
        
        public virtual tblFileUpload GettblFileUpload(Guid id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblFileUploadGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                tblFileUpload tblFileUpload =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblFileUpload = CreatetblFileUploadFromReader(reader);
                        reader.Close();
                    }
                }
                return tblFileUpload;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblFileUploadException, ex);
            }
        }
        
        #endregion

        #region GettblFileUploadList methods
        public virtual tblFileUploadCollection GettblFileUploadList(tblFileUploadColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblFileUploadGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblFileUploadCollection tblFileUploadCollection = new tblFileUploadCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblFileUpload tblFileUpload = CreatetblFileUploadFromReader(reader);
                        tblFileUploadCollection.Add(tblFileUpload);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblFileUploadCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblFileUploadListException, ex);
            }
        }
        
        public virtual tblFileUploadCollection GettblFileUploadList(tblFileUploadColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblFileUploadList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}